package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.service.storemanagementservice.RepertoryService;
import com.ljxt.dt.service.storemanagementservice.impl.RepertoryServiceImpl;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.RepertorySelectVO;
import com.ljxt.dt.vo.storemanagement.RepertoryVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-20 8:50
 * @copyright 老九学堂
 */
@WebServlet("/repertory.html")
public class Repertory extends HttpServlet {

    private RepertoryService admin = new RepertoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



        ServletUtil.forward(req,resp,"repertory","storeManagement");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RepertorySelectVO repertorySelectVO = ServletUtil.toObj(req,new RepertorySelectVO());
        //查询符合条件的商品
        Integer pageIndex = ServletUtil.getQueryInt(req,"pageIndex");
        Integer pageSize = ServletUtil.getQueryInt(req,"pageSize");
        if(pageIndex == 0){
            pageIndex =1;
        }
        if(pageSize == 0){
            pageSize =5;
        }
        PageVO pageVO = admin.selectRepertory(repertorySelectVO,pageIndex,pageSize);
        //返回pageVO

        pageVO.setSelectObj(repertorySelectVO);
        pageVO.setPageURL("repertory.html");
        JsonResult<PageVO> jsonResult = new JsonResult<>();
        jsonResult.setData(pageVO);
        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().write(objectMapper.writeValueAsString(jsonResult));
    }
}
